package com.ifast.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.album.domain.AlbumInfo;
import com.ifast.album.domain.AlbumPhoto;
import com.ifast.album.domain.AlbumPhotoMsg;
import com.ifast.album.service.AlbumInfoService;
import com.ifast.album.service.AlbumPhotoService;
import com.ifast.common.base.ApiBaseController;
import com.ifast.common.utils.Const;
import com.ifast.sys.domain.UserDO;
import com.luculent.util.extension.api.DataResult;
import com.luculent.util.extension.api.PageResult;
import com.luculent.util.extension.api.Result;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * <pre>
 *   相册接口
 * </pre>
 * 
 * <small>2019年4月10日15:28:01 | shíQíang㊚</small>
 */
@RestController
@RequestMapping("/api/album/")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiAlbumController extends ApiBaseController{
	
	@Autowired
	private AlbumInfoService alumInfoService;
	@Autowired
	private AlbumPhotoService albumPhotoService;
	
	@RequestMapping(value = "list.do")
    @ApiOperation( value = "相册-相册列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    
    public PageResult<?> list(
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        Page<AlbumInfo> page = new Page<>();
        page.setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 ); 
        return resolutionPage("list",new Param().setUser(getUser(token)).setPage(page).setNian(nian).setYue(yue).setRi(ri)); 
    } 

	@RequestMapping(value = "photoList.do")
    @ApiOperation( value = "普通相册-照片列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    
    public PageResult<?> photoList(
    		@ApiParam(name = "albumId", required = true, value = "相册id") @RequestParam(value = "albumId",required=true) String albumId,
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
		Page<AlbumPhotoMsg> page = new Page<>();
        page.setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 );
        return resolutionPage("photoList",new Param().setUser(getUser(token)).setPage(page).setNian(nian).setYue(yue).setRi(ri).setAlbumId(albumId));
    } 
	
	@RequestMapping(value = "photoLoopList.do")
    @ApiOperation( value = "轮播相册-照片列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    public PageResult<?> photoLoopList(HttpServletRequest request,
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageSize", required = false, value = "每页多少条") @RequestParam(value = "pageSize",required=false) String pageSize,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        Page<AlbumPhoto> page = new Page<>();
        page.setSize(StringUtils.isNotBlank(pageSize)?Integer.valueOf(pageSize):10).setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 ); 
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() +"/";
        return resolutionPage("photoLoopList",new Param().setUrl(basePath).setUser(getUser(token)).setPage(page).setNian(nian).setYue(yue).setRi(ri)); 
    }  
	
	@RequestMapping(value = "userPhotoLoopList.do")
    @ApiOperation( value = "用户轮播相册-照片列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    public PageResult<?> userPhotoLoopList(HttpServletRequest request,
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageSize", required = false, value = "每页多少条") @RequestParam(value = "pageSize",required=false) String pageSize,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        Page<AlbumPhoto> page = new Page<>();
        page.setSize(StringUtils.isNotBlank(pageSize)?Integer.valueOf(pageSize):10).setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 ); 
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() +"/";
        return resolutionPage("userPhotoLoopList",new Param().setUrl(basePath).setUser(getUser(token)).setPage(page).setNian(nian).setYue(yue).setRi(ri)); 
    } 
	
	@RequestMapping(value = "add.do")
    @ApiOperation( value = "相册-新建相册", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> add(
    		@ApiParam(name = "name", required = true, value = "相册名称") @RequestParam(value = "name",required=true) String name,
    		@ApiParam(name = "sort", required = false, value = "排序") @RequestParam(value = "sort",required=false) int sort,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	return resolution("add",null,new Param().setUser(getUser(token)).setName(name).setSort(sort));
    }  
	
	@RequestMapping(value = "edit.do")
    @ApiOperation( value = "相册-修改相册", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> edit(
    		@ApiParam(name = "albumId", required = true, value = "相册id") @RequestParam(value = "albumId",required=true) String albumId,
    		@ApiParam(name = "name", required = true, value = "相册名称") @RequestParam(value = "name",required=true) String name,
    		@ApiParam(name = "sort", required = false, value = "排序") @RequestParam(value = "sort",required=false) int sort,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	return resolution("edit",null,new Param().setUser(getUser(token)).setName(name).setSort(sort).setAlbumId(albumId));
    } 
	
	@RequestMapping(value = "remove.do")
    @ApiOperation( value = "普通相册-删除相册(逻辑删除)", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> remove(
    		@ApiParam(name = "albumId", required = true, value = "相册id") @RequestParam(value = "albumId",required=true) String albumId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
		return resolution("remove",null,new Param().setUser(getUser(token)).setAlbumId(albumId)); 
    }
	
	@RequestMapping(value = "setCover.do")
    @ApiOperation( value = "相册-设置封面（将某张照片设置成封面）", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> setCover(HttpServletRequest request,
    		@ApiParam(name = "albumId", required = true, value = "相册id") @RequestParam(value = "albumId",required=true) String albumId,
    		@ApiParam(name = "photoId", required = true, value = "照片id") @RequestParam(value = "photoId",required=true) String photoId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	return resolution("edit",null,new Param().setUser(getUser(token)).setAlbumId(albumId).setPhotoId(photoId));
    } 
	
	
	@RequestMapping(value = "photoLoopAdd.do")
    @ApiOperation( value = "轮播相册-照片添加", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> photoLoopAdd(HttpServletRequest request,
    		@ApiParam(name = "msg", required = false, value = "照片说明") @RequestParam(value = "msg",required=false) String msg,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	return resolution("photoLoopAdd",request,new Param().setUser(getUser(token)).setMsg(msg));
    }
	
    @RequestMapping(value = "photoAdd.do")
    @ApiOperation( value = "相册-照片添加", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> photoAdd(HttpServletRequest request,
    		@ApiParam(name = "albumId", required = true, value = "相册id") @RequestParam(value = "albumId",required=true) String albumId,
    		@ApiParam(name = "msg", required = false, value = "照片说明") @RequestParam(value = "msg",required=false) String msg,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	return resolution("photoAdd",request,new Param().setUser(getUser(token)).setAlbumId(albumId).setMsg(msg));
    } 
    
    @RequestMapping(value = "photoRemvoe.do")
    @ApiOperation( value = "相册-照片删除", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> photoRemvoe(
    		@ApiParam(name = "photoId", required = true, value = "照片id") @RequestParam(value = "photoId",required=true) String photoId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	return resolution("photoRemvoe",null,new Param().setPhotoId(photoId)); 
    }
    
    @RequestMapping(value = "photoEdit.do")
    @ApiOperation( value = "相册-照片编辑", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> photoEdit(HttpServletRequest request,
    		@ApiParam(name = "albumId", required = true, value = "相册id") @RequestParam(value = "albumId",required=true) String albumId,
    		@ApiParam(name = "photoId", required = true, value = "照片id") @RequestParam(value = "photoId",required=true) String photoId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
     	return resolution("photoEdit",request,new Param().setUser(getUser(token)).setAlbumId(albumId).setPhotoId(photoId)); 
    }
    
    @RequestMapping(value = "photoMsgEdit.do")
    @ApiOperation( value = "相册-照片说明编辑", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = DataResult.class)
    
    public DataResult<?> photoMsgEdit(HttpServletRequest request,
    		@ApiParam(name = "msgId", required = true, value = "msg的id") @RequestParam(value = "msgId",required=true) String msgId,
    		@ApiParam(name = "msg", required = false, value = "照片说明") @RequestParam(value = "msg",required=false) String msg,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
      	return resolution("photoMsgEdit",request,new Param().setUser(getUser(token)).setMsg(msg).setMsgId(msgId)); 
    } 
    /** 返回页面的*/
    @SuppressWarnings("unchecked")
	private PageResult<?> resolutionPage(String methodName,Param param){ 
		try {  
			switch (methodName) {
				case "list":
					return  alumInfoService.pageList(Const.album.album_nomal, param.getUser().getId().toString(),param.getUser().getDwCode(),param.getNian(),param.getYue(),param.getRi(),(Page<AlbumInfo>)param.getPage());
				case "photoList":
				     return albumPhotoService.pageNormalList(param.getUser().getId().toString(),param.getUser().getDwCode(),param.getAlbumId(),param.getNian(),param.getYue(),param.getRi(),(Page<AlbumPhotoMsg>)param.getPage());
				case "userPhotoLoopList":
				     return albumPhotoService.pageUserLoopList(param.getUser().getId().toString(),param.getUser().getDwCode(),param.getNian(),param.getYue(),param.getRi(),(Page<AlbumPhoto>)param.getPage(),param.getUrl());
				default:
					return albumPhotoService.pageLoopList(param.getUser().getId().toString(),param.getUser().getDwCode(),param.getNian(),param.getYue(),param.getRi(),(Page<AlbumPhoto>)param.getPage(),param.getUrl());
		    } 
		} catch (Exception e) {
        	e.printStackTrace(); 
			return PageResult.fail_msg("查询异常");
		}
	}
	/**将 try catch 抽离出来，上面看着工整了。不知道有个卵用*/
    private Result<?> resolution(String methodName,HttpServletRequest request, Param param){ 
		try {  
			switch (methodName) {
				case "add":
					return alumInfoService.add(Const.album.album_nomal, param.getUser().getId().toString(),  param.getUser().getDeptId(), param.getUser().getDwCode(),param.getName(),param.getSort());
				case "edit":
					return alumInfoService.edit(Const.album.album_nomal, param.getUser().getId().toString(),  param.getUser().getDwCode(), param.getAlbumId(), param.getName(),param.getSort());
				case "remove":
					return alumInfoService.remove(Const.album.album_nomal, param.getUser().getId().toString(),  param.getUser().getDwCode(), param.getAlbumId());
				case "photoLoopAdd":
					return albumPhotoService.upload(Const.album.album_loop,param.getUser().getId().toString(), param.getUser().getDeptId(), param.getUser().getDwCode(), param.getMsg(), request);
				case "photoAdd":
					return albumPhotoService.upload(param.getUser().getId().toString(),  param.getUser().getDwCode(), param.getAlbumId(), param.getMsg(), request);
				case "photoRemvoe":
					return albumPhotoService.remove(param.getPhotoId());
				case "photoEdit":
					return albumPhotoService.editPhoto(param.getUser().getId().toString(),  param.getUser().getDwCode(), param.getAlbumId(), param.getPhotoId(),request);     
				case "photoMsgEdit":
					return albumPhotoService.editMsg(param.getUser().getId().toString(),  param.getUser().getDwCode(),param.getMsgId(), param.getMsg());
				default:
					return albumPhotoService.cover(param.getAlbumId(), param.getPhotoId());
		    } 
		} catch (Exception e) {
			e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
	} 
   
    @Data 
    @Accessors(chain = true)
    private class Param{
    	private UserDO user;
    	private String name;
    	private int sort; 
    	private String albumId;
    	private String photoId;
    	private String msg;
    	private String msgId;
    	private String nian;
    	private String yue;
    	private String ri;
    	private Page<?> page;
    	private String url;
    }
}
